/*****************************************************************************************************
 * 
 * AUTORES: CARLOS VARELA ESCALONI (carlos.varela@udc.es) DENIS LAMELA MARTINEZ (denis.lamela@udc.es)
 * 
 *****************************************************************************************************/

package es.udc.muei.apm.sensores.carlos.denis;

import java.text.DecimalFormat;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import es.udc.muei.apm.sensores.R;

public class MainActivity extends Activity {

	private SensorManager mSensorManager;
	private Sensor sensorAcelerometro = null;
	private Sensor sensorMagnetometro = null;
	private Sensor sensorGiroscopio = null;

	private final SensorEventListener listener = new APMSensorListener();

	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.activity_main);

		final Button b1 = (Button) this.findViewById(R.id.btnConectar);
		final Button b2 = (Button) this.findViewById(R.id.btnDesconectar);

		b1.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				MainActivity.this.conectar();
				b1.setEnabled(false);
				b2.setEnabled(true);
			}
		});

		b2.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				MainActivity.this.desconectar();
				b1.setEnabled(true);
				b2.setEnabled(false);
			}
		});

		SensorManager sm = (SensorManager) this.getSystemService(Context.SENSOR_SERVICE);
		List<Sensor> sensorList = sm.getSensorList(Sensor.TYPE_ALL);
		StringBuilder sensorString = new StringBuilder("Sensors:\n");

		for (int i = 0; i < sensorList.size(); i++) {

			sensorString.append(sensorList.get(i).getName()).append(", \n");
		}

		// System.out.println(sensorString);
		this.mSensorManager = (SensorManager) this.getSystemService(Context.SENSOR_SERVICE);

		if (sensorString.indexOf("Acceleration") > 0) {
			this.sensorAcelerometro = this.mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
		}
		if (sensorString.indexOf("Gyroscope") > 0) {
			this.sensorGiroscopio = this.mSensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE);
		}
		if (sensorString.indexOf("Magnetic") > 0) {
			this.sensorMagnetometro = this.mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);
		}

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		this.getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	public void conectar() {

		if (this.sensorAcelerometro != null) {
			this.mSensorManager.registerListener(this.listener, this.sensorAcelerometro, SensorManager.SENSOR_DELAY_NORMAL);
		}
		if (this.sensorGiroscopio != null) {
			this.mSensorManager.registerListener(this.listener, this.sensorGiroscopio, SensorManager.SENSOR_DELAY_NORMAL);
		}
		if (this.sensorMagnetometro != null) {
			this.mSensorManager.registerListener(this.listener, this.sensorMagnetometro, SensorManager.SENSOR_DELAY_NORMAL);
		}
	}

	public void desconectar() {

		this.mSensorManager.unregisterListener(this.listener);
		TextView textView = null;
		textView = (TextView) this.findViewById(R.id.lblAcelerometro);
		textView.setText(this.getString(R.string.Acelerometro));
		textView = (TextView) this.findViewById(R.id.lblMagnetometro);
		textView.setText(this.getString(R.string.Magnetometro));
		textView = (TextView) this.findViewById(R.id.lblGiroscopio);
		textView.setText(this.getString(R.string.Giroscopio));
	}

	@Override
	protected void onResume() {

		final Button b1 = (Button) this.findViewById(R.id.btnConectar);

		if (!b1.isEnabled()) {
			this.conectar();
		}

		super.onResume();
	}

	@Override
	protected void onPause() {

		super.onPause();
	}

	@Override
	protected void onStop() {

		this.mSensorManager.unregisterListener(this.listener);
		super.onStop();
	}

	private class APMSensorListener implements SensorEventListener {

		@Override
		public final void onAccuracyChanged(Sensor sensor, int accuracy) {

			// Acciones a realizar si la precision del sensor cambia
		}

		@Override
		public final void onSensorChanged(SensorEvent event) {

			// Acciones a realizar en cuanto cambie el valor de un sensor
			DecimalFormat df = new DecimalFormat("#0.00");
			String valor = df.format(event.values[0]);
			TextView textView = null;

			if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
				textView = (TextView) MainActivity.this.findViewById(R.id.lblAcelerometro);
			}
			if (event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD) {
				textView = (TextView) MainActivity.this.findViewById(R.id.lblMagnetometro);
			}
			if (event.sensor.getType() == Sensor.TYPE_GYROSCOPE) {
				textView = (TextView) MainActivity.this.findViewById(R.id.lblGiroscopio);
			}
			if (textView != null) {
				textView.setText(valor);
			}
		}
	}
}
